<template>
  <div>
    <el-dialog :visible.sync="dialogVisible" @opened="init" :title="dialogTitle"
               :close-on-click-modal="false">
      <el-form :model="formData" ref="dataForm" size="small" :rules="formRules" label-width="auto">
        <el-row>
          <el-col :span="12">
            <el-form-item label="上级机构" prop="parent.id">
              <el-select v-model="formData.parent.id">
                <el-option v-for="item of deptList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机构名称" prop="name">
              <el-input v-model="formData.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机构类型" prop="type">
              <el-input v-model="formData.type"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="" prop="">
              <el-input></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="" prop="">
              <el-input></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="" prop="">
              <el-input></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template v-slot:footer>
        <el-button type="primary" @click="submitFormData">确定</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import SysDeptApi from '@views/modules/system/dept/SysDeptApi'

export default {
  name: 'SysDeptForm',
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    method: {
      type: String,
      required: true
    },
    dataId: {
      type: String
    },
    parentId: {
      type: String
    }
  },
  computed: {
    dialogVisible: {
      get () {
        return this.visible
      },
      set (value) {
        this.$emit('update:visible', value)
      }
    },
    dialogTitle () {
      if (this.method === 'view') {
        return '查看组织机构'
      } else if (this.method === 'add') {
        return '添加组织机构'
      } else if (this.method === 'edit') {
        return '修改组织机构'
      } else {
        return ''
      }
    }
  },
  mixins: [SysDeptApi],
  data () {
    return {
      formData: {
        name: '',
        type: '',
        sort: 0,
        parent: { id: '' }
      },
      formRules: {},
      deptList: []
    }
  },
  methods: {
    reset () {
      this.$refs.dataForm.resetFields()
    },
    init () {
      this.reset()
      this.loadDeptList()
      if (this.dataId) {
        this.loadFormData(this.dataId)
      }
    },
    loadFormData (id) {
      this.SysDeptApi.find(id).then(({ data }) => {
        this.$override(this.formData, data.data)
      })
    },
    submitFormData () {
      this.SysDeptApi.save(this.formData).then(({ data }) => {
        this.$message('保存成功')
        this.dialogVisible = false
        this.$emit('submit-success')
      })
    },
    loadDeptList () {
      this.SysDeptApi.list().then(({ data }) => {
        this.deptList = data.list
      })
    }
  }
}
</script>

<style scoped>

</style>
